﻿using System;
using System.Configuration;
using CoreVirtuoso.Config;
using VirtuosoDal.Config;
using VirtuosoDal.Repositories.RdfRepositories.Interfaces.VirtuosoCore;
using VirtuosoDal.Repositories.RdfRepositories.VirtuosoCore;

namespace SetupStore
{
   class Program
   {
      static void Main(string[] args)
      {
         try
         {
            //Console.WriteLine("User: ");
            //string user = Console.ReadLine();

            //Console.WriteLine("Password: ");
            //string password = Console.ReadLine();

            IGraphRepository graphRepository = new GraphRepository();

            Console.WriteLine("Started clearing EventsSchemaGraph");
            graphRepository.ClearGraph(Namespaces.EventsOntologyNamespace);
            Console.WriteLine("Finished clearing EventsSchemaGrpah");

            //Console.WriteLine("Started clearing LgdSchemaGraph");
            //graphRepository.ClearGraph(Namespaces.LgdOntologyNamespace);
            //Console.WriteLine("Finished clearing LgdSchemaGraph");

            //Console.WriteLine("Started uploading LgdVocabulary ontology");
            //string lgdVocabularyPath = ConfigurationManager.AppSettings["LgdVocabularyPath"];
            //if (String.IsNullOrEmpty(lgdVocabularyPath))
            //   throw new Exception("LgdVocabularyPath should be set in config's AppSettings section");
            //graphRepository.UploadGraph(lgdVocabularyPath, Namespaces.LgdOntologyNamespace);
            //Console.WriteLine("Finished uploading LgdVocabulary ontology");

            Console.WriteLine("Started uploading Events ontology");
            string eventsOntologyPath = ConfigurationManager.AppSettings["EventsOntologyPath"];
            if (String.IsNullOrEmpty(eventsOntologyPath))
               throw new Exception("EventsOntologyPath should be set in config's AppSettings section");
            graphRepository.UploadGraph(eventsOntologyPath, Namespaces.EventsOntologyNamespace);
            Console.WriteLine("Finished uploading EventsSchemaGraph");

            Console.WriteLine("Started uploading RDFS ontology");
            string rdfsOntologyPath = ConfigurationManager.AppSettings["RdfsOntologyPath"];
            if (String.IsNullOrEmpty(rdfsOntologyPath))
               throw new Exception("RdfsOntologyPath should be set in config's AppSettings section");
            graphRepository.UploadGraph(rdfsOntologyPath, Namespaces.EventsOntologyNamespace);
            Console.WriteLine("Finished uploading RDFS ontology");

            Console.WriteLine("Started uploading OWL ontology");
            string owlOntologyPath = ConfigurationManager.AppSettings["OwlOntologyPath"];
            if (String.IsNullOrEmpty(owlOntologyPath))
               throw new Exception("OwlOntologyPath should be set in config's AppSettings section");
            graphRepository.UploadGraph(owlOntologyPath, Namespaces.EventsOntologyNamespace);
            Console.WriteLine("Finished uploading OWL ontology");

            IRuleSetsRepository ruleSetsRepository = new RuleSetsRepository();

            ruleSetsRepository.AddGraphToRuleSet(RuleSets.EventsRuleSet, Namespaces.EventsOntologyNamespace);

            ruleSetsRepository.AddGraphToRuleSet(RuleSets.LgdVocabularyRuleSet, Namespaces.LgdOntologyNamespace);

            Console.Write("Are you sure, you want to clear the content? Type yes or no: ");
            string yesOrNo = Console.ReadLine();
            if (yesOrNo.ToUpper() == "YES")
            {
               graphRepository.ClearGraph(Namespaces.EventsOntologyNamespace);
               graphRepository.ClearGraph(Namespaces.GeoLabelsNamespace);

               var sequenceInitialValue = 1;

               ISequenceRepository sequenceRepository = new SequenceRepository();

               int sequenceSetResult = sequenceRepository.SequenceSet(Sequences.EventsSequenceSet, sequenceInitialValue);
               if (sequenceSetResult != sequenceInitialValue)
                  throw new Exception("SequenceSet wasn't executed correctly");

               sequenceSetResult = sequenceRepository.SequenceSet(Sequences.UsersSequenceSet, sequenceInitialValue);
               if (sequenceSetResult != sequenceInitialValue)
                  throw new Exception("SequenceSet wasn't executed correctly");

               sequenceSetResult = sequenceRepository.SequenceSet(Sequences.GeoLabelsSequenceSet, sequenceInitialValue);
               if (sequenceSetResult != sequenceInitialValue)
                  throw new Exception("SequenceSet wasn't executed correclty");

               //UsersRepository repo = new UsersRepository();
               //repo.ClearData();

               Console.WriteLine("Content was cleared");
            }

            IFullTextSearchRepository fullTextSearchRepository = new FullTextSearchRepository();

            fullTextSearchRepository.AddToFullTextSearch(Namespaces.EventsContentNamespace, null);
            Console.WriteLine("Added EventsContentGraph to full text search");

            fullTextSearchRepository.AddToFullTextSearch(Namespaces.GeoNamesOntologyNamespace, "http://www.geonames.org/ontology#name");
            Console.WriteLine("Added GeoNamesGraph/name to full text search");
            fullTextSearchRepository.AddToFullTextSearch(Namespaces.GeoNamesOntologyNamespace, "http://www.geonames.org/ontology#alternateName");
            Console.WriteLine("Added GeoNamesGraph/alternateName to full text search");
            fullTextSearchRepository.AddToFullTextSearch(Namespaces.GeoNamesOntologyNamespace, "http://www.geonames.org/ontology#preferredName");
            Console.WriteLine("Added GeoNamesGraph/preferredName to full text search");

            //basicRdfRepository.AddToFullTextSearch(Config.LgdContentGraph, "http://linkedgeodata.org/vocabulary#name", null);

            fullTextSearchRepository.SetFreeTextIndexAutoUpdate();

            Console.WriteLine("Success");
         }
         catch (Exception e)
         {
            Console.WriteLine(e.Message);
         }

         Console.ReadLine();

      }
   }
}
